﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;
using edowork.com.Model;

namespace WebInvoicingsys.system.baseset.productdata
{
    public partial class product_add : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        private void Reset()
        {
            txt_productcode.Text = "";
            txt_productname.Text = "";
            txt_productshortname.Text = "";
            uc_from_goodsclass.Reset();
            uc_from_goodsunit.Reset();
            uc_from_goodsspec.Reset();
            uc_from_stack.Reset();
            uc_from_guard.Reset();
            txt_maxqty.Text = "";
            txt_minqty.Text = "";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ;
            }
        }

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            alert_msg = "";

            decimal maxqty = 0, minqty = 0;

            if (Decimal.TryParse(txt_maxqty.Text.Trim(), out maxqty) == false)
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入最高库存量或输入的最高库存量格式有误");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_maxqty.Focus();

                return;
            }

            if (Decimal.TryParse(txt_minqty.Text.Trim(), out minqty) == false)
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入最低库存量或输入的最低库存量格式有误");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_minqty.Focus();

                return;
            }


            edowork.com.Model.productInfo productInfo = new productInfo();

            productInfo.Productcode = txt_productcode.Text.Trim();
            productInfo.Productname = txt_productname.Text.Trim();
            productInfo.Productshortname = txt_productshortname.Text.Trim();
            productInfo.Goodsclassid = uc_from_goodsclass.getValue();
            productInfo.Goodsunitid = uc_from_goodsunit.getValue();
            productInfo.Goodsspecid = uc_from_goodsspec.getValue();
            productInfo.Stackid = uc_from_stack.getValue();
            productInfo.Guardid = uc_from_guard.getValue();
            productInfo.Maxqty = maxqty;
            productInfo.Minqty = minqty;
            productInfo.Sysdate = DateTime.Now.ToString("yyyy-MM-dd");

            if (String.IsNullOrEmpty(productInfo.Productcode))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入货品条码");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_productcode.Focus();

                return;
            }

            if (String.IsNullOrEmpty(productInfo.Productname))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入货品全称");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_productname.Focus();

                return;
            }

            if (String.IsNullOrEmpty(productInfo.Productshortname))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入货品简称");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_productshortname.Focus();

                return;
            }

            if (String.IsNullOrEmpty(productInfo.Goodsclassid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择货品类别");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(productInfo.Goodsunitid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择货品单位");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(productInfo.Goodsspecid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择货品规格");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(productInfo.Stackid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择堆放方式");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(productInfo.Guardid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择防护方式");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            //保存数据入数据库
            using (edowork.com.SQL2005.TranUtil tranutil = new TranUtil(Session["connectionString"].ToString()))
            {
                System.Data.SqlClient.SqlTransaction tran = null;

                try
                {
                    tran = tranutil.GetSqlTransaction();
                }
                catch (Exception er)
                {
                    alert_msg = String.Format(Scripts.alert_warning, er.Message);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                edowork.com.SQL2005.Execute exec = new Execute(Session["connectionString"].ToString());

                string sql = "";
                int result = -1;

                //保存入product表
                sql = String.Format(productInfo.SQL_Insert(), 
                                    productInfo.Productcode, productInfo.Productname, productInfo.Productshortname, 
                                    productInfo.Goodsclassid, productInfo.Goodsunitid, productInfo.Goodsspecid, 
                                    productInfo.Stackid, productInfo.Guardid, productInfo.Maxqty, 
                                    productInfo.Minqty, productInfo.Sysdate);

                result = exec.ExecSQLQueryInt(sql, ref alert_msg, tran);

                if (result <= 0)
                {
                    tran.Rollback();
                    tranutil.CloseConnection();

                    alert_msg = String.Format(Scripts.alert_warning, alert_msg);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                tran.Commit();
                tranutil.CloseConnection();
            }

            Reset();

            alert_msg = String.Format(Scripts.alert_success, Errormsg.success_Save);

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
        }

        //重置
        protected void btnReset_Click(object sender, EventArgs e)
        {
            Reset();
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("/system/baseset/productdata/product.aspx");
        }
    }
}